{extend name="property/public/insidePageBase" /}
{block name="title"}平台协议列表{/block}
{block name="css"}
{__block__}
<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
        height: 34px;
        line-height: 34px;
        padding: 0 8px;
    }
</style>
{/block}
{block name="body"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">关键字搜索</label>
                    <div class="layui-input-inline">
                        <input type="text" name="keywords"  placeholder="请输入关键字查询" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">按类型搜索</label>
                    <div class="layui-inline">
                        <select name="pact_type">
                            <option value="">请选择</option>
                            {foreach $pactType as $k=>$v}
                            <option value="{$k}">{$v}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                </div>
            </div>
        </form>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                {if $addAuth}
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                {/if}
            </div>
        </script>
        <table class="layui-hide" id="templateList" lay-filter="templateList"></table>
        <script type="text/html" id="tableBar">
            <div class="layui-btn-group">
                {if $editAuth} <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">编辑</a>{/if}
                {if $delAuth} <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>{/if}
            </div>
        </script>
        <script type="text/html" id="content_tpl">
            <button class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="checkContent">查看协议</button>
        </script>
        <script type="text/html" id="pact-type-tpl">
            {{# if(d.pact_type === 1) { }}<span class="layui-badge layui-bg-orange">用户协议</span>{{# } }}
            {{# if(d.pact_type === 2) { }}<span class="layui-badge layui-bg-green">隐私协议</span>{{# } }}
            {{# if(d.pact_type === 3) { }}<span class="layui-badge layui-bg-black">注销协议</span>{{# } }}
            {{# if(d.pact_type === 5) { }}<span class="layui-badge layui-bg-blue">支付协议</span>{{# } }}
            {{# if(d.pact_type === 6) { }}<span class="layui-badge layui-bg-cyan">发布协议</span>{{# } }}
        </script>
    </div>
</div>
{/block}
{block name="js"}
{__block__}
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery, form = layui.form,table = layui.table;
        var tableId = 'templateList';
        table.render({
            elem: '#'+tableId,
            id:tableId,
            url: '{:url("propertySystemPactList")}',
            toolbar: '#toolbar',
            cols: [[
                {field: 'pact_type', width: 108, title: '协议类型', templet:'#pact-type-tpl', sort: true},
                {field: '', width: 100, title: '协议内容', templet:'#content_tpl'},
                {field: 'add_time', width: 160, title: '创建时间', sort: true},
                {field: 'edit_time', width: 160, title: '编辑时间', sort: true}
                {if $editAuth || $delAuth}
                , {title: '操作', minWidth: 200, toolbar: '#tableBar'}
                {/if}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            height: 'full-80'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.reload(tableId, { page: { curr: 1 }, where: data.field }, 'data');return false;
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar('+tableId+')', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var url = '{:url("propertySystemPactAdd")}';
                var index = layer.open({title: '添加协议',type: 2,shade: 0.2,maxmin:true,area: ['700px', '95%'],content: url, btnAlign: 'c' , btn: ['确定', '取消'], yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index] , submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    layer.close(index); //关闭弹层
                                    layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox('+tableId+')', function (obj) {
            console.log(obj)
        });
        table.on('tool('+tableId+')', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var url = '{:url("propertySystemPactEdit")}?id='+data.id;
                var index = layer.open({title: '编辑 ' , type: 2,shade: 0.2,maxmin:true,area: ['700px', '95%'],content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index] , submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) { var field = data.field; //获取提交的字段
                            var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    layer.close(index); //关闭弹层
                                    layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
                return false;
            } else if (obj.event === 'checkContent') {
                parent.layer.open({title:'查看'+' '+data.pact_type, content:data.content, area:['800px','95%'], btnAlign: 'c',shadeClose:true, btn:['关闭协议详情面'] })
            } else if (obj.event === 'delete') {
                var url = '{:url("propertySystemPactDel")}';
                layer.confirm('是否确认删除? ', {icon: 3}, function () {
                    var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                    $.post(url, {id: data.id}, function (res) {
                        layer.close(loadAdd);
                        if (res.code === 0) {
                            table.reload(tableId); //数据刷新
                            layer.msg(res.msg, {icon: 6, offset: '15px'});
                        } else {
                            layer.msg(res.msg, {icon: 5, offset: '15px'});
                        }
                    });
                });
                return false;
            }
        });
    });
</script>
{/block}